/*
 * Copyright 2007 Matt Jensen
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package org.jtell.internal;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jtell.*;
import org.jtell.config.Configuration;

import java.util.List;

/**
 * <p>
 * <code>JTellContextImpl</code> is the concrete implementation of the {@link JTellContext} interface. It is the core
 * object through which client code registers and unregisters listeners and dispatches events.
 * </p>
 * <p>
 * <strong>Thread Safety</strong><br/>
 * Instances of this class are safe for multithreaded access.
 * </p>
 */
class JTellContextImpl implements JTellContext
{
    private static Log LOG = LogFactory.getLog(JTellContextImpl.class);

    /**
     * <p>
     * The configuration source. 
     * </p>
     */
    private final Configuration m_configuration;

    /**
     * <p>
     * The event metadata sources.
     * </p>
     */
    private final List<EventMetadataSource> m_eventMetadataSources;

    /**
     * <p>
     * The event notifier.
     * </p>
     */
    private final EventNotifier m_eventNotifier;

    /**
     * <p>
     * The event sink registry.
     * </p>
     */
    private final EventSinkRegistry m_eventSinkRegistry;

    /**
     * <p>
     * The event sink sources. 
     * </p>
     */
    private final List<EventSinkSource> m_eventSinkSources;

    /**
     * <p>
     * Construct a {@link JTellContextImpl} instance.
     * </p>
     *
     * @param configuration the configuration.
     * @param eventMetadataSources the event metadata sources.
     * @param eventSinkRegistry the event sink registry.
     * @param eventSinkSources the event sink sources.
     */
    public JTellContextImpl(final Configuration configuration,
                            final List<EventMetadataSource> eventMetadataSources,
                            final EventSinkRegistry eventSinkRegistry,
                            final List<EventSinkSource> eventSinkSources)
    {
        super();
        Guard.notNull("configuration", configuration);
        Guard.notNull("eventMetadataSources", eventMetadataSources);
        Guard.notNull("eventSinkRegistry", eventSinkRegistry);
        Guard.notNullOrEmpty("eventSinkSources", eventSinkSources);

        m_configuration = configuration;
        m_eventMetadataSources = eventMetadataSources;
        m_eventSinkRegistry = eventSinkRegistry;
        m_eventSinkSources = eventSinkSources;

        m_eventNotifier = new EventNotifierImpl();
    }

    /* inherit javadoc */
    public EventChannel createChannel()
    {
        final EventChannel result = new EventChannelImpl(m_configuration, m_eventMetadataSources, m_eventSinkRegistry,
                m_eventSinkSources, m_eventNotifier);
        if (LOG.isDebugEnabled())
        {
            LOG.debug(String.format("Returning new top-level event channel [%s].", result));
        }
        return result;
    }
}
